#! /usr/bin/python
import os
from sets import Set

list_file = r'./so_list'
result_file = r'./result.txt'
find_str = r'Loaded symbols for '
ldd_cmd_str = 'ldd '
ssl_so_str = 'libssl.so'
crypto_so_str = 'libcrypto.so'

if __name__ == '__main__':
  try:
    dir_set = Set([])
    with open(list_file, 'r') as file:
      for each_line in file.readlines():
        if (each_line.startswith(find_str)):
          each_line = each_line[each_line.find(find_str) + len(find_str):each_line.rfind(r'/')].strip()
          dir_set.add(each_line)
    for each in dir_set:
      print(each)
    count = 0;
    final_set = Set([])
    for each_dir in dir_set:
      if (os.path.isdir(each_dir)):
        for each_file in os.listdir(each_dir):
          if (os.path.isfile(os.path.join(each_dir, each_file))):
            if (each_file.find(r'.so') >= 0):
              ldd_rslt = os.popen(ldd_cmd_str + os.path.join(each_dir, each_file)).readlines()
              count += 1
              for each_item in ldd_rslt:
                if (each_item.find(ssl_so_str) >= 0 or each_item.find(crypto_so_str) >= 0):
                  final_set.add(os.path.join(each_dir, each_file))
    print('Totally process ' + str(count) + ' .so files!')
    with open(result_file, 'w') as rslt_file:
      for each in final_set:
        rslt_file.write(each + '\n')
  except Exception as ex:
    print(str(ex))
